<template>
	<view class="wrapper">
		<view class="shade" @click="cancel"></view>
		<view class="add-popup">
			<view class="popup-title">
				<text>开启{{title}}</text>
			</view>
			<view class="popup-content">
				<view class="form-wrap">
					<view class="form-item">
						<input type="text" v-model="name" placeholder="请输入房间名称" />
					</view>
				</view>
			</view>
			<view class="popup-footer">
				<button type="default" size="mini" @click="cancel">取消</button>
				<button type="primary" size="mini" @click="enter">确定</button>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				type: '',
				title: '房间',
				myInfo: {},
				name: '',
			}
		},
		onLoad(options) {
			this.type = options.type
			switch(this.type) {
				case '0':
					this.title = '视频相亲';
					break;
				case '1':
					this.title = '专属相亲';
					break;
				case '2':
					this.title = '单人直播';
					break;
			}
			try {
				const myInfo = uni.getStorageSync('myInfo');
				if (myInfo) {
					this.myInfo = myInfo;
				}
			} catch (e) {
				console.log(e)
			}
		},
		onUnload() {
			
		},
		onReady() {
			
		},
		methods: {
			cancel() {
				uni.navigateBack({
					delta: 1
				})
			},
			enter() {
				let type = '0';
				if (this.type == '0') {
					type = '1';
				}
				if (this.type == '1') {
					type = '2';
				}
				if (this.type == '2') {
					type = '7';
				}
				if (this.name == '') {
					uni.showToast({
						title: '请输入房间名称',
						position: 'bottom',
						icon: 'none'
					});
					return false;
				}
				if(this.myInfo.ban==4){
					uni.showToast({
						title: '您被禁止开播',
						position: 'bottom',
						icon: 'none'
					});
					return false;
				}
				if (this.type == '2') {
					uni.showLoading({
						title: ''
					});
					this.$api.api.ajaxData({
						method: 'V2.LiveCreate',
						uid: this.myInfo.id,
						token: this.myInfo.token,
						name: this.name,
						type: type,
						file: '',
						password: ''
					}, res => {
						uni.hideLoading();
						if (res.status) {
							uni.showToast({
								title: '创建房间成功',
								position: 'bottom',
								icon: 'none'
							});
							this.$tim.createGroup({
								name: this.name,
								type: this.TIM.TYPES.GRP_AVCHATROOM,
								groupID: this.IDBEFORE + res.data.rome.id,
							}).then(imResponse => {
								console.log('------>音视频聊天室创建成功:', imResponse.data);
								this.$tim.joinGroup({
									groupID: this.IDBEFORE + res.data.rome.id,
									type: this.TIM.TYPES.GRP_AVCHATROOM,
								}).then(imResponse => {
									console.log('申请加群', JSON.stringify(imResponse.data))
								}).catch(imError => {
									console.warn('申请加群失败', imError);
								});
							}).catch(imError => {
								console.warn('------>音视频聊天室创建失败:', imError);
							});
							let live = {
								room: {
									id: res.data.rome.id,
									name: res.data.rome.name,
								},
								character: 'anchor',
								type: type,
							}
							uni.redirectTo({
								url: '../live/seven_room?live=' + encodeURIComponent(JSON.stringify(live))
							});
						} else {
							uni.showToast({
								title: res.msg,
								position: 'bottom',
								icon: 'none'
							});
						}
					})
					return false;
				}
				uni.showLoading({
					title: ''
				});
				this.$api.api.ajaxData({
					method: 'V1.LoveCreate',
					uid: this.myInfo.id,
					token: this.myInfo.token,
					name: this.name,
					type: type,
				}, res => {
					uni.hideLoading();
					uni.showToast({
						title: res.msg,
						position: 'bottom',
						icon: 'none'
					});
					if (res.status) {
						this.$tim.createGroup({
							name: this.name,
							type: this.TIM.TYPES.GRP_AVCHATROOM,
							groupID: this.IDBEFORE + res.data.rome.id,
						}).then(imResponse => {
							console.log('------>音视频聊天室创建成功:', imResponse.data.group);
							this.$tim.joinGroup({
								groupID: this.IDBEFORE + res.data.rome.id,
								type: this.TIM.TYPES.GRP_AVCHATROOM,
							}).then(imResponse => {
								console.log('------>申请加群', imResponse.data)
							}).catch(imError => {
								console.warn('------>申请加群失败', imError);
							});
						}).catch(imError => {
							console.warn('------>音视频聊天室创建失败:', imError);
						});
						getApp().globalData.invite_list = res.data.list;
						let live = {
							room: {
								id: res.data.rome.id,
								name: res.data.rome.name,
							},
							character: 'anchor',
							rose: '',
							gift: res.data.gift,
							url: res.data.play,
							count: 1,
							type: type,
							anchor: {
								id: res.extra.info.id,
								name: res.extra.info.name,
								avatar: res.extra.info.avatar,
								sex: res.extra.info.sex,
								age: res.extra.info.age,
								city: res.extra.info.city,
							},
						}
						uni.redirectTo({
							url: '../live/live_room?live=' + encodeURIComponent(JSON.stringify(live))
						});
					}
				}, err => {
					uni.hideLoading();
					uni.showToast({
						title: '网络错误，请稍后再试',
						position: 'bottom',
						icon: 'none'
					});
				})
			}
		}
	}
</script>

<style lang="scss">
	@import '../../common/common.scss';
	page {
		width: 100%;
		height: 100%;
		background: none;
	}
	.wrapper {
		width: 100%;
		height: 100%;
		position: relative;
	}
	.shade {
		background-color: rgba(0,0,0,0.5);
		position: absolute;
		left: 0;
		right: 0;
		top: 0;
		bottom: 0;
	}
	.add-popup {
		display: flex;
		flex-direction: column;
		width: 560rpx;
		height: 360rpx;
		background-color: #FFFFFF;
		border-radius: 20rpx;
		margin-left: -280rpx;
		margin-top: -380rpx;
		position: absolute;
		left: 50%;
		top: 50%;
		.popup-title {
			display: flex;
			justify-content: center;
			align-items: center;
			height: 120rpx;
			text {
				font-size: 36rpx;
				color: #333333;
			}
		}
		.popup-content {
			display: flex;
			justify-content: center;
			align-items: center;
			padding-bottom: 20rpx;
		}
		.form-wrap {
			display: flex;
			flex-direction: column;
			justify-content: center;
			align-items: center;
			width: 480rpx;
			height: 100rpx;
			.form-item {
				display: flex;
				justify-content: flex-start;
				align-items: center;
				width: 400rpx;
				height: 100rpx;
				border-bottom: 1rpx solid #efefef;
				padding: 0 10rpx;
				box-sizing: border-box;
				input {
					flex: 1;
					font-size: 28rpx;
				}
			}
		}
		.popup-footer {
			display: flex;
			justify-content: space-around;
			align-items: center;
			height: 120rpx;
		}
	}
</style>
